package myJs.user

import myJs.myPkg.jquery.$
import myJs.myPkg.{PlotlyConfigOptions, myPlotly}
import scalatags.Text.all.s
import shared.Pojo.CurSeqData
import shared.plotly.{Pie, Scatter}
import shared.plotly.element.Color.StringColor
import shared.plotly.element.{AxisType, HoverInfo, Marker, ScatterMode}
import shared.plotly.element.ScatterMode.Lines
import shared.plotly.layout.HoverMode.Closest
import shared.plotly.layout.{Axis, Font, Layout, Margin}

/**
 * Created by yz on 3/6/2020
 */
trait SampleManagePlot {

  def strDetailPlot(strSeqDatas: List[CurSeqData]) = {
    val chartId = "strDetailChart"
    val genotypes = strSeqDatas.map(_.genotype).distinct
    val xs = genotypes.indices.toList
    val genotypeXMap = genotypes.zip(xs).toMap
    val step = 0.25
    val plotData = strSeqDatas.map { seqData =>
      val genotype = seqData.genotype
      val index = genotypeXMap(genotype)
      val xMap = Map(index.toDouble -> seqData.reads)
      val xGenotypeMap = Map(index.toDouble -> seqData.genotype)
      val linesX = (BigDecimal(index - 0.5) to BigDecimal(index + 0.5) by BigDecimal(step)).toList.map(_.toDouble)
      val linesY = linesX.map { x =>
        xMap.getOrElse(x, "0")
      }
      val texts = linesX.map { case x =>
        if (xMap.isDefinedAt(x)) {
          val genotype = xGenotypeMap(x)
          val read = xMap(x)
          s"Allele Name:${genotype}<br>Reads:${read}"
        } else ""
      }
      Scatter(
        values = linesX,
        secondValues = linesY,
        marker = Marker(
          color = new StringColor("#1F77B4")
        ),
        hoverinfo = HoverInfo.Text,
        mode = ScatterMode(Lines),
        text = texts,
      )
    }

    val layout = Layout(
      title = s"",
      xaxis = Axis(
        title = s"",
        `type` = AxisType.Linear,
        tickangle = 0.0,
        tickvals = xs,
        ticktext = genotypes,
        autorange = true,
        tickfont = Font(size = 10),
        zeroline = false,
      ),
      yaxis = Axis(
        title = s"Number of reads",
        autorange = true,
      ),
      margin = Margin(b = 20,
        t = 20,
        r = 0),
      width = 400,
      height = 300,
      hovermode = Closest,
      dragmode = false,
      showlegend = false
    )
    val config = PlotlyConfigOptions.displayModeBar(false)
    myPlotly.newPlot(s"${
      chartId
    }", plotData, layout, config)
  }

  def snpDetailPlot(seqDatas: List[CurSeqData]) = {
    val xs = seqDatas.map(_.genotype)
    val reads = seqDatas.map(_.reads)
    val plotData = Seq(
      Pie(
        labels = xs,
        values = reads,
        marker = Marker(
        ),
        hoverinfo = HoverInfo.Y,
        width = 0.5
      )
    )

    val layout = Layout(
      title = s"",
      xaxis = Axis(
        title = s"",
        `type` = AxisType.Category,
        tickangle = 0.0,
        autorange = true,
        tickfont = Font(size = 10)
      ),
      yaxis = Axis(
        title = s"Number of reads",
        autorange = true
      ),
      margin = Margin(b = 0,
        t = 0,
        l = 0,
        r = 0),
      width = 400,
      height = 400,
      hovermode = Closest,
      dragmode = false
    )
    val config = PlotlyConfigOptions.displayModeBar(false)
    myPlotly.newPlot($("#snpModal #detailChart"), plotData, layout, config)
  }


}
